﻿using System;
using CardLogistics.Model.Sys;
using CardLogistics.Web.Components;

namespace CardLogistics.Web.MainPage.SystemMgr.SysUser
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.Sys.Sys_SysUserInfoData DAL = new DAL.Sys.Sys_SysUserInfoData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDLL(ROLEID, "SYS_SYSROLES", "FULLNAME", "ROLEID", " and DELETEMARK='1'");
                BindTxt();

            }
        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = "";
                string logContent = "";
                if (IsEdit)
                {
                    result = DAL.Update(EncapsulationModel());
                    logContent = "修改用户信息,用户账户:" + EncapsulationModel().USERNAME;
                }
                else
                {
                    result = DAL.Add(EncapsulationModel());
                    logContent = "添加用户信息,用户账户:" + EncapsulationModel().USERNAME;
                }

                if (result.Split(',')[0] == "ok")
                {
                    InsertLog(logContent);
                    ReturnListInfo();
                }
                else if (result.Split(',')[1] == "-1")
                {
                    ShowMessageBox("登录名重复,请重新输入！");
                    USERNAME.Focus();
                }
                else
                {
                    ShowMessageBox("操作失败！" + result.Split(',')[1]);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        /// <summary>
        /// 获取对象
        /// </summary>
        /// <returns></returns>
        private Sys_SysUserInfo EncapsulationModel()
        {
            Sys_SysUserInfo model = new Sys_SysUserInfo();
            model = GetWebControls<Sys_SysUserInfo>(this.Page);
            model.CREATEUSER = LoggedUser.ID;
            model.USERPWD = new DESC().Encrypto("0000");

            if (USERSTATUS.Checked)
                model.USERSTATUS = 1;
            else
                model.USERSTATUS = 0;

            if (IsEdit)
                model.ID = Request.QueryString["id"];
            return model;
        }
        /// <summary>
        /// 绑定值
        /// </summary>
        private void BindTxt()
        {
            string id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                Sys_SysUserInfo Data = DAL.GetModel(id);
                SetWebControls(this.Page, Data);
                if (Data.USERNAME.ToLower().Equals("admin"))
                {
                    USERNAME.Attributes.Add("readonly", "readonly");
                }
            }
        }
    }
}